<?php
/*
* Author: Jason ZHENG <zmj522008@hotmil.com>
* Date  : 2018/05/23
* Class : Feedback 反馈消息
*/
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Feedback extends Controller
{ 
	public function index()
	{ 
		echo 'Feedback Controller'; 
	}

	//创建一个反馈消息
	public function create(Request $request)
	{
		$postData = $request->post();
		$rules = [
            "title|标题"		=> "require|max:50",
            "content|内容"	=> "require",
            "type|类型"		=> "require|number",
		];

		if($this->validate($postData, $rules) === true)
		{
			if(Db::table('feedback')->insert([
					'user_id'		=> Session::get('user.id'),
					'title' 		=> $postData['title'],
					'content'		=> $postData['content'],
					'type' 			=> $postData['type'],
					'create_time' 	=> date('Y:m:d H:i:s', time()),
				]))
				return ['code' => 666, 'msg' => 'ok', 'data' => null];
			else
				return ['code' => 10001, 'msg' => 'database error', 'data' => null];
		}else
		{
			return ['code' => 10010, 'msg' => $this->validate($postData, $rules), 'data' => null];
		}
	}

	// 获取反馈消息类型
	public function types()
	{
		$types = Db::table('feedback_types')
				->where(['state' => 0])
				->field('label, value')
				->select();
		return ['code' => 666, 'msg' => 'ok', 'data' => $types];
	}

	// 获取反馈消息列表
	public function lists($page=1)
	{
		$lists = Db::table('feedback')
				->alias('f')
				->join('feedback_types ft', 'f.type = ft.label', 'LEFT' )
				->where(['f.user_id' => Session::get('user.id'), 'f.is_delete' => 0 ])
				->field('f.*, ft.value type_value')
				->limit(($page-1)*20,$page*20-1)
				->select();
		$count = Db::table('feedback')
				->alias('f')
				->join('feedback_types ft', 'f.type = ft.label', 'LEFT' )
				->where(['f.user_id' => Session::get('user.id'), 'f.is_delete' => 0 ])
				->count();

		return ['code' => 666, 'msg' => 'feedback lists', 'data' => ['count'=> $count, 'lists' => $lists]];
	}

	// 删除一条反馈消息
	public function delete(Request $request)
	{
		$postData = $request->post();
		$rules = [
            "id|id"	=> "require|number",
		];

		if($this->validate($postData, $rules) === true)
		{
			if(! empty(
				Db::table('feedback')
					->where([
							'user_id'		=> Session::get('user.id'),
							'id' 			=> $postData['id'],
							'is_delete'     => 0,
							])
					->find()
				)){

				Db::table('feedback')
					->where([
							'user_id'		=> Session::get('user.id'),
							'id' 			=> $postData['id'],
							])
					->update(['is_delete' => 1]);
				return ['code' => 666, 'msg' => '删除成功', 'data' => null];
			}
				
			else
				return ['code' => 10001, 'msg' => '消息不存在或已删除', 'data' => null];
		}else
		{
			return ['code' => 10010, 'msg' => $this->validate($postData, $rules), 'data' => null];
		}
	}

	// 获取一条反馈消息
	public function get(Request $request)
	{
		$postData = $request->post();
		$rules = [
            "id|id"	=> "require|number",
		];

		if($this->validate($postData, $rules) === true)
		{
			if(! empty(
				Db::table('feedback')
					->where([
							'user_id'		=> Session::get('user.id'),
							'id' 			=> $postData['id'],
							'is_delete'     => 0,
							])
					->find()
				)){

				$feedback = Db::table('feedback')
					->alias('f')
					->join('feedback_types ft', 'f.type = ft.label', 'LEFT' )
					->where([
							'f.user_id'		=> Session::get('user.id'),
							'f.id' 			=> $postData['id'],
							'f.is_delete'     => 0,
							])
					->field('f.*, ft.value type_value')
					->select();
				return ['code' => 666, 'msg' => 'ok', 'data' => $feedback];
			}
				
			else
				return ['code' => 10001, 'msg' => '消息不存在或已删除', 'data' => null];
		}else
		{
			return ['code' => 10010, 'msg' => $this->validate($postData, $rules), 'data' => null];
		}
	}

}